public class Solution {
    public int strStr(String haystack, String needle) {
        int hlen = haystack.length();
        int nlen = needle.length();
        if(nlen == 0) {
            return 0;
        }
        int j = 0;
        for(int i = 0; i < hlen; i++) {
            if(haystack.charAt(i) == needle.charAt(j)) {
                j++;
            } else {
                i = i - j;
                j = 0;
            }
            if(j == nlen) {
                return i-j+1;
            }
        }
        return -1;
    }
}